<template>
    <div class="update-room" id="printObj">
    <el-row type="flex" justify="end">
          <el-button v-print="printObj" size="small" type="primary">打印</el-button>
   </el-row>
     <el-form :model="roomform" :rules="rules" ref="roomsForm">
       <el-form-item label="房间名" label-width="100px" prop="name">
         <el-input v-model="roomform.name" style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="房间号" label-width="100px"  prop="num">
         <el-input v-model.number="roomform.num" style="width:80%"></el-input>
      </el-form-item>
       <el-form-item label="价格" label-width="100px" prop="price">
         <el-input v-model.number="roomform.price" style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="类型" label-width="100px" prop="type">    
           <el-select v-model.number="roomform.type">
          <el-option
          v-for="item in roomTypes"
          :value="item.id"
          :key="item.id"
          :label="item.value"
         />
           </el-select>
      </el-form-item>
      <el-form-item label="状态" label-width="100px" prop="state" >
          <el-select v-model.number="roomform.state">
          <el-option
           v-show="item.id>0"
          v-for="item in roomState"
          :value="item.id"
          :key="item.id"
          :label="item.value"
         />
           </el-select>
      </el-form-item>
      <el-form-item label="简介" label-width="100px" prop="baseinfo">
         <el-input type="textarea" v-model="roomform.baseinfo" style="width:80%" />
      </el-form-item>
    </el-form>
    <el-row justify="center" type="flex">
      <el-col :span="16">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel" type="warning">重置</el-button>
        <el-button size="small" @click="$router.push('/roomMng')" type="success">返回房间列表</el-button>
      </el-col>
    </el-row>
    </div>
</template>

<script>
import Rooms from '@/utils/constant/rooms'
import {getroom,getRoomList,repairRoom} from '@/api/rooms'
export default {
data(){
  // 校验房间号是否存在
 const checkNum=async(rule, value, callback)=>{
    const rst=await getRoomList()
    // console.log(rst);
    const flag=rst.filter(item=>item.id!=this.roomId).some(item=>item.num===value)
    return flag?callback(new Error('该房间号已存在')):callback()
    }
    // 校验房间名是否存在
    const checkname=async(rule, value, callback)=>{
    const rst=await getRoomList()
    const flag=rst.filter(item=>item.id!=this.roomId).some(item=>item.name===value)
    return flag?callback(new Error('该房间号已存在')):callback()
    }
  return {
    roomId:this.$route.params.id,
    roomTypes:Rooms.type,
    roomState:Rooms.state,
    printObj:"#printObj",
    roomform:{
      num:null,
      name:null,
      price:null,
      type:null,
      state:null,
      baseinfo:null
    },
    rules:{
      num:[ { required: true, message: '请输入房间号', trigger: 'blur' },
      { type: 'number', message: '请输入正确的房间号', trigger: 'blur' },
      {validator:checkNum,trigger:'blur'}
      ],
      name:[{ required: true, message: '请输入房间名', trigger: 'blur' },
       {validator:checkname,trigger:'blur'}],
      price:[{ required: true, message: '请输入房间价格', trigger: 'blur' },
      { type: 'number', message: '请输入正确的价格', trigger: 'blur' }],
      type:[{ required: true, message: '请选择房间类型', trigger: 'blur' },
      { type: 'number', message: '请输入正确的类型', trigger: 'blur' }],
      state:[{ required: true, message: '请选择房间状态', trigger: 'blur' },
      { type: 'number', message: '请选择正确的状态', trigger: 'blur' }],
      baseinfo:[{ required: true, message: '请填写房间基本信息', trigger: 'blur' }]
    }
  }
},
created(){
  console.log(this.roomId);
  this.getroom()
},
methods:{
  // 获取当前房间信息
 async getroom(){
   this.roomform= await getroom(this.roomId)
  },
  async btnOK(){
      // 确认添加
      await this.$refs.roomsForm.validate()
      await repairRoom(this.roomform)
      this.$message.success('修改房间基本信息成功')
      this.$router.back()
    },
    btnCancel(){
      // 删除
      this.$refs.roomsForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.update-room {
  max-width: 90%;
  margin: 30px auto;
}
</style>